package com.zx.BookSeaReader.greendao.service;

import com.zx.BookSeaReader.greendao.GreenDaoManager;
import com.zx.BookSeaReader.greendao.entity.Book;
import com.zx.BookSeaReader.greendao.entity.BookLabelBean;
import com.zx.BookSeaReader.greendao.entity.UserBean;
import com.zx.BookSeaReader.greendao.gen.BookLabelBeanDao;
import com.zx.BookSeaReader.util.Constant;
import com.zx.BookSeaReader.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: ZX
 * @Time: 2020/4/20
 * @Description: 功能描述
 */
public class LabelService extends BaseService {
    /**
     * @Author: ZX
     * @Time: 2020/2/15
     * @Description: 该用户是否存在
     */
    public  List<BookLabelBean> getLabels(String username,String bookId) {
        BookLabelBeanDao bookLabelBeanDao = GreenDaoManager.getInstance().getSession().getBookLabelBeanDao();
        List<BookLabelBean> Labels=new ArrayList<>();
        Labels = bookLabelBeanDao.queryBuilder()
                .where(BookLabelBeanDao.Properties.Username.eq(username), BookLabelBeanDao.Properties.BookId.eq(bookId))
                .list();
        if (Labels.size() > 0) {
            return Labels;
        } else {
            Labels=new ArrayList<>();
        }
        return Labels;
    }

    /**
     * @Author: ZX
     * @Time: 2020/4/15
     * @Description: 添加书签
     */
    public void addLabel(Book book, UserBean userBean, String title, String chapterName){
        BookLabelBean labelBean=new BookLabelBean();
        labelBean.setBookId(book.getId());
        labelBean.setLabelTime( StringUtils.
                dateConvert(System.currentTimeMillis(), Constant.FORMAT_BOOK_DATE));
        labelBean.setChapterNum(book.getHisttoryChapterNum());
        labelBean.setReadPosition(book.getLastReadPosition());
        labelBean.setTitle(title);
        labelBean.setChapterName(chapterName);
        labelBean.setUsername(userBean.getUsername());
        addEntity(labelBean);
    }

    /**
     * @Author: ZX
     * @Time: 2020/4/15
     * @Description: 删除书签
     */
    public void deleteLabel(BookLabelBean labelBean){
        deleteEntity(labelBean);
    }

    /**
     * @Author: ZX
     * @Time: 2020/4/15
     * @Description: 更新书签
     */
    public void updateLabel(BookLabelBean labelBean){
        updateEntity(labelBean);
    }



}
